'use strict';

// signup controller
app.controller('SignupFormController', ['$scope', '$state', 'toaster', 'ajax', 'logger',
	function($scope, $state, toaster, ajax, logger) {
		$scope.user = {};
		$scope.company = {};
		$scope.checkcode = null;

		// 验证密码
		$scope.validateUserPwd = function() {
			var flag = false;
			var pwd = $scope.user.userPassword;
			var repwd = $scope.user.userRePassword;
			var patten = /^[A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\_\+\`\~\-\=]{6,22}$/;
			if (pwd != repwd) {
				toaster.pop('warning', '错误', '两次密码输入不一致！');
				return flag;
			}
			if (!patten.test(pwd)) {
				toaster.pop('warning', '错误', '密码格式为6到22位字母、数字和字符~!@#$%^&*()_+`-=！');
				return flag;
			} else {
				return !flag;
			}
		};

		// 比对验证码
		$scope.validateCode = function() {
			if ($scope.checkcode == null || $scope.checkcode != $scope.user.phoneCode) {
				toaster.pop('warning', '错误', '验证码错误！');
				return false;
			} else {
				return true;
			}
		};

		// 发送验证码，此时对手机号验重
		$scope.sendCode = function() {
			var num = $scope.user.phone;
			if (!/^1[3|4|5|7|8][0-9]\d{8}$/.test(num)) {
				toaster.pop('warning', '错误', '手机号码格式不正确！');
				return;
			}

			var querier = new Req.Querier({
				resource: 'signup/' + $scope.user.phone + '/validatePhone'
			});
			ajax.q(querier).then(function(result) { // 调用承诺API获取数据 .resolve
				if (result.code == 404) {
					null;
				} else if (result.code == 200) {
					toaster.pop('error', '错误', '手机号已存在！');
					return;
				} else {
					toaster.pop('error', '错误', '服务器错误');
					return;
				}
				var btn = $('#send');
				var count = 60;
				var resend = setInterval(function() {
					if (count > 0) {
						btn.val(count + "秒后可重新获取");
						count--;
					} else {
						clearInterval(resend);
						btn.val("获取验证码").removeAttr('disabled style');
						btn.removeAttr('ng-disabled');
					}
				}, 1000);
				btn.attr('disabled', true).css('cursor', 'not-allowed');
				btn.attr('ng-disabled', true);

				var querier1 = new Req.Querier({
					resource: 'signup/' + $scope.user.phone + '/sendCode'
				});
				var promise = ajax.q(querier1); // 同步调用，获得承诺接口
				promise.then(function(result) { // 调用承诺API获取数据 .resolve
					if (result.code == 200) {
						$scope.checkcode = result.result;
					} else {
						toaster.pop('error', '错误', '发送验证码失败！');
					}
				}, function(data) { // 处理错误 .reject
				});
			}, function(data) { // 处理错误 .reject
			});
		};

		// 跳转到登陆页面
		$scope.gotoSignin = function() {
			$state.go('access.signin');
		};

		// 个人用户注册
		$scope.personalSignup = function() {
			var patten = /^[a-zA-Z]{1}[0-9a-zA-Z_]{5,22}$/;
			if (!patten.test($scope.user.userName)) {
				toaster.pop('warning', '错误', '用户名格式为6到22位字母和数字！');
				return;
			}
			if ($scope.validateUserPwd() && $scope.validateCode()) {
				var querier = new Req.Querier({
					resource: 'signup/' + $scope.user.userName + '/validateUserName'
				});
				ajax.q(querier).then(function(result) { // 调用承诺API获取数据 .resolve
					if (result.code == 404) {
						var inserter = new Req.Inserter({
							resource: 'signup/personalRegist',
							datalist: [$scope.user]
						});
						var promise = ajax.c(inserter);
						promise.then(function(result) {
							if (result.code == 200) {
								$(".signup .step2").addClass("active").siblings().removeClass("active");
								$(".signup .step_list li").eq(1).addClass("active").siblings().removeClass("active");
								$(".signup .step").removeClass("col-sm-8");
								$(".signup .login-area").hide();
								setTimeout("$('.step2 span').empty().append('2')", 1000);
								setTimeout("$('.step2 span').empty().append('1')", 2000);
								setTimeout("$('.step2 span').empty().append('0')", 3000);
								setTimeout("window.location.replace('#/access/signin/')", 4000);
								logger.usrLog('用户注册', '个人注册',
									{ username: $scope.user.userName }, '注册成功', { userName: $scope.user.userName });
							} else {
								switch (result.code) {
								case -1: toaster.pop('error', '错误', '用户名不合法！'); break;
								case -2: toaster.pop('error', '错误', '包含不允许注册的词语！'); break;
								case -3: toaster.pop('error', '错误', '用户名已经存在！'); break;
								case -4: toaster.pop('error', '错误', 'email 格式有误！'); break;
								case -5: toaster.pop('error', '错误', 'email 不允许注册！'); break;
								case -6: toaster.pop('error', '错误', '该 email 已经被注册！'); break;
								default: toaster.pop('error', '错误', '注册失败！'); break;
								}
							}
						}, function(data) {
							toaster.pop('error', '错误', '注册失败！');
						});
					} else if (result.code == 200) {
						toaster.pop('error', '错误', '用户名已存在！');
					} else {
						toaster.pop('error', '错误', '服务器错误');
					}
				}, function(data) { // 处理错误 .reject
				});
			}
		};
	}]);